package com.shop.controller;

import com.shop.common.FastDFSClientUtils;
import com.shop.service.ImageServiceRest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/image")
public class ImageController {
    @Autowired
    private ImageServiceRest imageServiceRest;
    static Logger log = LoggerFactory.getLogger(ImageController.class);
    /***
     * 通过type获取图片 1:轮播图 2：广告图
     * @param type
     * @return
     */
    @GetMapping("getImageByType/{type}")
    public Dto getImageByType(@PathVariable("type") int type){
        return DtoUtil.returnDataSuccess(imageServiceRest.getImageByType(type));
    }

    /**
     * 图片文件上传
     * @param file
     * @return
     */
    @PostMapping("uploadIma")
    public Dto uploadIma(MultipartFile file){
        if(file.isEmpty()){
            return DtoUtil.returnFail("文件夹为空",500);
        }
        try{
            return DtoUtil.returnSuccess("上传成功",
                    FastDFSClientUtils.upload(file.getBytes(),file.getOriginalFilename()));
        }catch (IOException e){
            e.printStackTrace();
        }
        return DtoUtil.returnFail("上传失败，请联系管理员",50);
    }
    //埋点事件处理
    @GetMapping("log")
    public String log(String str){
        log.error(str);
        //javaIO操作
        return "SUCCESS";
    }
}
